package com.rc.saas.tenant.controller.tenant;

import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.common.utils.FileHandler;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.component.UploadImg;
import com.rc.saas.tenant.config.UpYunConfig;
import com.rc.saas.tenant.model.tenant.Tenant;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.service.tenant.TenantService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.upyun.WebUploader;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.text.MessageFormat;

/**
 * 修改logo
 * Created by frog on 2021/1/2
 */
@Controller
@RequestMapping("/tenant/user")
public class TenantUserModifyLogoController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(TenantUserModifyLogoController.class);

    private String prefix = "/templates/tenant/user";

    @Autowired
    private UpYunConfig upYunConfig;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private UploadImg uploadImg;

    /**
     * 打开logo页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/modifyLogo", method = RequestMethod.GET)
    public String modifyLogo(Model model) {
        TenantUser user = ShiroUtils.getSessionUser();
        Tenant tenant = tenantService.selectByTenantCode(user.getTenantCode());
        model.addAttribute("tenantLogoImg", tenant.getTenantLogoImg());
        model.addAttribute("uploadPath", upYunConfig.getDownloadRoot());
        return prefix + "/modifyLogo";
    }


    /**
     * 保存logo
     *
     * @return
     */
    @RequestMapping(value = "/saveLogo", method = RequestMethod.POST)
    @ResponseBody()
    public TResponse saveLogo(@RequestParam(value = "file1", required = false) MultipartFile file1) {
        try {
            TenantUser user = ShiroUtils.getSessionUser();
            Tenant tenant = tenantService.selectByTenantCode(user.getTenantCode());
            if (file1.getSize() != 0) {
                TResponse tResponse = uploadImg.uploadImg(user.getTenantCode(), file1);
                if (tResponse.getStatus() == 200) {
                    tenant.setTenantLogoImg((String) tResponse.getData());
                    tenantService.updateByPrimaryKeySelective(tenant);
                } else {
                    return tResponse;
                }
            }
        } catch (Exception e) {
            return TResponse.FAIL("操作失败");
        }
        return TResponse.SUCCESS();
    }



}
